#! /bin/sh
#
# crond   Start/Stop/Install the PostgreSQL postmaster daemon.
# v1.0 2005-09-21 20:28:39 rurban@x-ray.at
#
# chkconfig: 2345 90 60
# description: PostgreSQL is an advanced Object-Relational database management system.
# processname: postmaster
# config: /usr/share/postgresql7.4/data/postgresql.conf
# pidfile: /usr/share/postgresql7.4/data/postgresql.pid

PREFIX=/usr
BINDIR=${PREFIX}/lib/postgresql7.4/bin
PGDATA=${PREFIX}/share/postgresql7.4/data
CONFIG=$PGDATA/postgresql.conf
PIDFILE=$PGDATA/postgresql.pid
LOGFILE=/var/log/postgresql7.4.log
SOCKETDIR=/tmp
PATH=${BINDIR}:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# Source function library.
if [ -f /etc/init.d/functions ] ; then
  . /etc/init.d/functions
fi

RETVAL=0

# See how we were called.

name="PostgreSQL 7.4"
prog="postmaster"
DAEMON="${BINDIR}/${prog}"
POSTMASTER_ARGS="-D $PGDATA -k $SOCKETDIR -i"
INITDB_ARGS="-E SQL_ASCII --locale=C -D $PGDATA"

test -f $DAEMON || exit 0

# Source configuration
if [ -f /etc/sysconfig/$prog ] ; then
    . /etc/sysconfig/$prog
fi

set -x

start() {
        echo -n $"Starting $name: "
        check_cygserver
	# check if cygrunsrv process
        cygrunsrv --start "$name"
        RETVAL=$?
        [ $RETVAL -eq 0 ] && touch $PIDFILE  && echo "done."
	if [ $RETVAL -ne 0 ]; then 
	    echo "ERROR service start FAILED"; 
	    tail $LOGFILE; 
	fi
        return $RETVAL
}

startlocal() {
	echo -n $"Starting $DAEMON locally: "
	chmod u+rw $LOGFILE $PIDFILE
	${BINDIR}/pg_ctl start -D $PGDATA -o "-i" -l $LOGFILE
	RETVAL=$?
	[ $RETVAL -eq 0 ] && touch $PIDFILE  && echo "done."
        return $RETVAL
}

stop() {
        echo -n $"Stopping $name: "
	cygrunsrv --stop "$name"
        if test -r $PIDFILE; then
            kill `cat $PIDFILE` 2> /dev/null || echo -n " failed"
        else
            echo -n " no PID file"
        fi
	# this really needs a long time to stop.
        RETVAL=$?
        echo "."
        [ $RETVAL -eq 0 ] && rm -f $PIDFILE && echo "done."
        return $RETVAL
}

rhstatus() {
        # service status
        cygrunsrv -Q "$name"
}

restart() {
        echo -n $"Restarting $name: "
        $0 stop
        sleep 1
        $0 start
        echo "done."
}

initdb() {
	${BINDIR}/initdb -U SYSTEM ${INITDB_ARGS} && echo "done."
	mkdir ${SOCKETDIR}
	chmod -R g+rw $PGDATA
	chgrp -R 18 $PGDATA
}

install() {
        check_cygserver
        echo -n $"Installing $name daemon: "
	touch $LOGFILE $PIDFILE
	chmod g+rw $LOGFILE $PIDFILE
	chgrp 18 $LOGFILE $PIDFILE
	cygrunsrv --install "$name" --path $DAEMON \
	    --args "${POSTMASTER_ARGS}" -e CYGWIN=server \
	    --disp "CYGWIN `$DAEMON --version`" \
	    --dep cygserver --termsig INT --shutdown
        echo "done."
}
uninstall() {
        echo -n $"Uninstalling $prog daemon: "
	stop
	cygrunsrv --remove "$name"
        echo "done."
}
reload() {
        echo -n $"Reloading $prog daemon configuration: "
	echo "unsupported. Try $0 restart"
	return 1

	/usr/bin/kill -HUP `cat $PIDFILE`
        RETVAL=$?
        [ $RETVAL -eq 0 ] && echo "done."
        RETVAL=$?
        return $RETVAL
}

check_cygserver() {
    # Check for running cygserver processes first. 
    if ps -ef | grep -v grep | grep -q cygserver
    then
        echo -n "- cygserver OK - " 
        # echo "OK cygserver running"
    else
  	echo "ERROR cygserver must be started"
  	echo cygrunsrv -S cygserver
	exit 1
    fi
    # check for CYGWIN containing server
    if [ -n "${CYGWIN%server}" ] 
    then
  	echo "ERROR CYGWIN must contain server for cygserver to work properly"
	exit 1
    fi
}

case "$1" in
  start) 	start  ;;
  startlocal)   startlocal ;;
  stop)         stop  ;;
  restart)      restart  ;;
  reload)       reload   ;;
  initdb)       initdb   ;;
  install)      install  ;;
  uninstall)    uninstall ;;
  status)       rhstatus  ;;
  condrestart)
        [ -f $PIDFILE ] && restart || :
        ;;
  *)
        echo $"Usage: $0 {start|stop|status|initdb|install|uninstall|startlocal|restart|condrestart}"
        exit 1
esac

exit $?
